package com.myhome.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.myhome.bean.User;
import com.myhome.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import com.myhome.model.User12;

@Controller
public class UserController {

    private Logger logger = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "catAopTest")
    @ResponseBody
    public String catAopTest() {
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000 * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                test01();
            }
        }).start();

        return "wert";
    }

    private void test01() {
        logger.info("11111111111111");
        userService.findAll();
        logger.info("22222222222222");
        userService.catTestMethod();
        logger.info("333333333333");
        try {
            userService.insertUser(new User());
        } catch (Exception ex) {
            System.out.println("...................出错了");
            ex.printStackTrace();
            logger.error(ex);
        }
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    @RequestMapping(value = "test123")
    public String loginForm12() {
        System.out.println("123");
        logger.info("1234.............................5555");
        logger.error("TEST..........................");
        // 动态跳转页面
        return "registerForm";
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    public String register(@ModelAttribute User12 user, Model model) {
        logger.info(user);
        model.addAttribute("user", user);
        return "success";
    }

}
